from functools import lru_cache
from collections import defaultdict
def solution():
n, m = map(int, input().split())
s = input()
t = input()
leftMost = [None]*len(t)
j = 0
for i in range(len(s)):
if j < len(t) and s[i] == t[j]:
leftMost[j] = (i)
j += 1
rightMost = [None]*len(t)
j = len(t)-1
for i in range(len(s))[::-1]:
if j >= 0 and s[i] == t[j]:
rightMost[j] = i
j -= 1
mx = 0
for i in range(1, len(leftMost)):
ans = rightMost[i] - leftMost[i-1]
mx = max(mx, ans)
print(mx)
solution()
/* {وَقُلْ رَبِّ زِدْنِي عِلْمًاً} */
#include<bits/stdc++.h>
using namespace std;
#define TY cin.tie(0), cout.tie(0), cin.sync_with_stdio(0), cout.sync_with_stdio(0);
#define ll long long int
#define f(nn) for(ll i = 0; i < (nn); i++)
#define frev(nn) for (ll i = nn - 1; i >= 0; --i)
void jo() {
ll n,m;cin>>n>>m;
string s,t;cin>>s>>t;
vector<int>pref(n+1),suf(n+1);
int j = 0;
for (int i = 0; i < n; ++i) {
if(s[i]==t[j]){
pref[j] = i,j++;
if(j==m)break;
}
}
j--;
frev(n){
if(s[i]==t[j]){
suf[j] = i,j--;
if(j==-1)break;
}
}
int ans = 0;
f(m)
ans=max(suf[i+1]-pref[i],ans);
cout<<ans<<"\n";
}
int main() {
TY
jo();
}
1409A - Yet Another Two Integers Problem | 977A - Wrong Subtraction |
263A - Beautiful Matrix | 180C - Letter |
151A - Soft Drinking | 1352A - Sum of Round Numbers |
281A - Word Capitalization | 1646A - Square Counting |
266A - Stones on the Table | 61A - Ultra-Fast Mathematician |
148A - Insomnia cure | 1650A - Deletions of Two Adjacent Letters |
1512A - Spy Detected | 282A - Bit++ |
69A - Young Physicist | 1651A - Playoff |
734A - Anton and Danik | 1300B - Assigning to Classes |
1647A - Madoka and Math Dad | 710A - King Moves |
1131A - Sea Battle | 118A - String Task |
236A - Boy or Girl | 271A - Beautiful Year |
520B - Two Buttons | 231A - Team |
479C - Exams | 1030A - In Search of an Easy Problem |
158A - Next Round | 71A - Way Too Long Words |